﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;

namespace ConvertData
{

    public class ConvertContentForDownloadButton : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                switch ((int)value)
                {
                    case 0:
                        return "/Images/Icon/download.png";
                    case 1:
                        return "/Images/Icon/download.png";
                    case 2:
                        return "/Images/Icon/download_disable.png";
                }

            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }

    public class ConvertEnableForDownloadButton : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                switch ((int)value)
                {
                    case 0:
                        return true;
                    case 1:
                        return false;
                    case 2:
                        return false;
                }

            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }

    public class ConvertVisibilityForDownloadButton : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                switch ((int)value)
                {
                    case 0:
                        return Visibility.Visible;
                    case 1:
                        return Visibility.Collapsed;
                    case 2:
                        return Visibility.Visible;
                }

            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }

    public class BooleanToVisibilityConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
        {
            if (value is int)
            {
                switch ((int)value)
                {
                    case 0:
                        return Visibility.Collapsed;
                    case 1:
                        return Visibility.Visible;
                    case 2:
                        return Visibility.Collapsed;
                }
                return Visibility.Collapsed;
            }
            else if (value is Boolean)
            {
                return (Boolean)value;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }

    public class InvertBooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Boolean)
            {
                // original
                //return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;

                // edited for this app
                return ((bool)value) ? Visibility.Collapsed : Visibility.Visible;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class ItemContainerToZIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var itemContainer = (DependencyObject)value;

            var itemsControl = FindAncestor<ItemsControl>(itemContainer);
            int index = itemsControl.ItemContainerGenerator.IndexFromContainer(itemContainer);
            return -index;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public T FindAncestor<T>(DependencyObject obj) where T : DependencyObject
        {
            var tmp = VisualTreeHelper.GetParent(obj);
            while (tmp != null && !(tmp is T))
            {
                tmp = VisualTreeHelper.GetParent(tmp);
            }
            return (T)tmp;
        }
    }

    public class TransferConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is long)
            {
                return String.Format(" {0} KB.",
                        ((decimal)((long)value / 1024)).ToString());
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }

    public class FileNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string)
            {
                string fileName = value.ToString();
                string[] param = fileName.Split('@');
                // param[0] = chapterId
                // param[1] = manga name
                // param[2] = chapter file (Ex: Chapter_001.zip)

                return param[1] + " - " + param[2];
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            return value;
        }
    }
}
